package grades

import (
	"fmt"
	"sync"
)

//学生
type Student struct {
	ID                  int
	FirstName, LastName string
	Grades              []Grade
}

//计算学生平均分
func (s Student) Average() float32 {
	var result float32
	for _, grade := range s.Grades {
		result += grade.Score
	}
	return result / float32(len(s.Grades))
}

//学生列表
type Students []Student

//学生流标变量,加锁保证并发访问的安全
var (
	students      Students
	studentsMutex sync.Mutex
)

//通过ID查找学生
func (ss Students) GetByID(id int) (*Student, error) {
	for i := range ss {
		if ss[i].ID == id {
			return &ss[i], nil
		}
	}
	return nil, fmt.Errorf("Student with ID %s notfound", id)
}

//成绩类型
type GradeType string

const (
	GradeQuiz = GradeType("Quiz")
	GradeTest = GradeType("Test")
	GradeExam = GradeType("Exam")
)

//成绩
type Grade struct {
	Title string
	Type  GradeType
	Score float32
}
